/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
#define max(a,b) (a>b?a:b)
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    ListNode* p1 = l1, * p2 = l2;
    int s1 = 0, s2 = 0;
    int a1[110] = { 0 }, a2[110] = { 0 }, a3[110] = { 0 };
    while (p1)
    {
        a1[s1++] = p1->val;
        p1 = p1->next;
    }
    while (p2)
    {
        a2[s2++] = p2->val;
        p2 = p2->next;
    }
    for (int i = 0; i < max(s1, s2); i++)
    {
        a3[i] = a3[i] + a1[i] + a2[i];
        if (a3[i] > 9)
        {
            a3[i] %= 10;
            a3[i + 1]++;
        }
    }
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode* cur = head;
    for (int i = 0; i < max(s1, s2) + 1; i++)
    {
        if (i == max(s1, s2) && a3[i] == 0)
            break;
        ListNode* tmp = (ListNode*)malloc(sizeof(ListNode));
        tmp->val = a3[i];
        tmp->next = NULL;
        cur->next = tmp;
        cur = cur->next;
    }
    return head->next;
}